require "http"

class Api::V1::ShopsController < Api::V1::BaseController
  skip_before_action :authenticate_request!, only: [:index, :show, :near, :spus]
  before_action :authenticate_request, only: [:index, :show, :near, :spus]

  def index
    
  end

  def show
    @shop = Shop.find params[:id]
  end

  def near
    if params[:latitude].blank? || params[:longitude].blank?
      render json: { result: false, message: '缺少参数.' }, status: :ok and return
    end
    @shops= Shop.all
    if @current_area.presence
      @shops = shops_where_area(@shops, @current_area.id)
    end
    @location = "#{params[:latitude]},#{params[:longitude]}"
    @shops = @shops.within(5, origin: [params[:latitude], params[:longitude]]).where(state: :opening).order("distance asc")
    if params[:shop_type_id].presence
      @shops = @shops.where(shop_type_id: params[:shop_type_id])
    end
    @shops = @shops.page(params[:page]).per(params[:per])
  end

  def create
    if @current_user.nickname.blank?
      render json: { result: false, message: '请先完善用户名字.' }, status: :ok and return
    end
    @shoper = Shoper.where(phone: @current_user.phone).first
    if @shoper.blank?
      @password = rand(999999).to_s
      @shoper = Shoper.new(phone: @current_user.phone, password: @password)
      if @shoper.save
        @shop = Shop.new(name: "#{@current_user.nickname}的店", phone: @current_user.phone, user_id: @current_user.id)
        if @shop.save
          @shoper.update_column('shop_id', @shop.id)
          fare_template = FareTemplate.create(shop_id: @shop.id, name: "默认模版", fare_method: :express)
          FareMode.create(fare_template_id: fare_template.id, first_n_piece: 1, first_amount: 0.0, second_n_piece: 0, second_amount: 0.0, is_default: true)
        else
          render json: { result: false, message: '该手机开通门店管理员失败.' }, status: :ok and return
        end
        @reponse = Qcloud::Sms.single_sender(@current_user.phone, "268467", ["#{@current_user.nickname}", "#{@password}"])
        Rails.logger.error @reponse
        render json: { result: true, message: '开通成功，管理员初始密码会通过短信方式发送到你的手机.' }, status: :ok
      else
        render json: { result: false, message: '该手机开通门店管理员失败.' }, status: :ok and return
      end
    else
      if @shoper.shop.blank?
        @shop = Shop.new(name: "#{@current_user.nickname}的店", phone: @current_user.phone)
        if @shop.save
          @shoper.update_column('shop_id', @shop.id)
        else
          render json: { result: false, message: '该手机开通门店管理员失败.' }, status: :ok and return
        end
        render json: { result: true, message: '开通门店成功.', data: { open: false, id: @shoper.shop.id } }, status: :ok
      else
        if @shoper.shop.pending?
          render json: { result: true, message: '该手机已经开通门店.', data: { open: false, id: @shoper.shop.id } }, status: :ok and return
        else
          render json: { result: true, message: '该门店已通过审核.', data: { open: true, id: @shoper.shop.id } }, status: :ok and return
        end
      end
    end
  end

  def validate_info
    @shoper = Shoper.where(phone: @current_user.phone).first
    if @shoper.blank?
      render json: { result: false, message: '该手机未注册门店管理员.' }, status: :ok and return
    end
    @shop = @shoper.shop
    if @shop.blank?
      render json: { result: false, message: '该手机未开通门店.' }, status: :ok and return
    end
  end
  
  def validate
    @shoper = Shoper.where(phone: @current_user.phone).first
    if @shoper.blank?
      render json: { result: false, message: '该手机未注册门店管理员.' }, status: :ok and return
    end
    @shop = @shoper.shop
    if @shop.blank?
      render json: { result: false, message: '该手机未开通门店.' }, status: :ok and return
    end
    if @shop.update shop_params
      render json: { result: true, message: '上传成功.' }, status: :ok
    else
      render json: { result: false, message: '上传失败.' }, status: :ok
    end
  end

  def spus
    @spus = Spu.where(shop_id: params[:id], state: :onsale, published: true)
    if params[:promotion].presence && params[:promotion]
      @spus = @spus.where(promotion: true)
    end
    if params[:hot].presence && params[:hot]
      @spus = @spus.order("sales_volume DESC")
    end
    if params[:newest].presence && params[:newest]
      @spus = @spus.order("created_at DESC")
    else
      @spus = @spus.order("created_at DESC")
    end
    @spus = @spus.page(params[:page]).per(params[:per])
  end
  
  def update_stars
    @shop = Shop.find params[:id]
    if @shop.save
      render json: { result: true, message: '评价成功.' }, status: :ok
    else
      render json: { result: false, message: '评价失败.' }, status: :ok
    end
  end
  
  private

  def shop_params
    params.require(:shop).permit(:name, :phone, :contact_name, :contact_phone, :province_id, :city_id, :district_id, :address, :avatar, :idcard_front, :idcard_back, :license)
  end
  
end
